#!/usr/bin/env python
#-*- coding: utf8 -*-

import urllib2
import urllib
import os
import time

import mp3play

def get_channel_list():
    channel_list=[]
    url='http://douban.fm/radio'
    headers={
            'Accept':'text/html, application/xhtml+xml, */*',
            'Accept-Language':'zh-CN',
            'User-Agent': 'Mozilla/5.0 (X11 yorks )',
            #'Accept-Encoding': 'gzip, deflate',
            'Host': 'douban.fm',
            'Connection': 'Keep-Alive',
            'Cookie':'bid="Etpoyx3j0BA"; ac="1304248680"'
            }
    req = urllib2.Request(url=url, headers=headers)
    conn=urllib2.urlopen(req)
    res = conn.read()
    #print res
    conn.close()
    channels = res.split(r'channels:')[1].split(r'};')[0]
    channel_str=urllib.unquote(channels).strip()[1:-1]
    #print channel_str
    '''
    '[{"channel_id":0,"name":"私人频道"},{"channel_id":1,"name":"华语"},{"channel_id":6,"name":"粤语"},{"channel_id":2,"name":"欧美"},{"channel_id":27,"name":"古典"},{"channel_id":26,"name":"豆瓣音乐人"},{"channel_id":22,"name":"法语"},{"channel_id":17,"name":"日语"},{"channel_id":18,"name":"韩语"},{"channel_id":13,"name":"爵士"},{"channel_id":14,"name":"电子"},{"channel_id":16,"name":"R&B"},{"channel_id":15,"name":"说唱"},{"channel_id":7,"name":"摇滚"},{"channel_id":8,"name":"民谣"},{"channel_id":10,"name":"电影原声"},{"channel_id":9,"name":"轻音乐"},{"channel_id":20,"name":"女声"},{"channel_id":3,"name":"七零"},{"channel_id":4,"name":"八零"},{"channel_id":5,"name":"九零"},{"channel_id":19,"name":"Puma+Social"},{"channel_id":30,"name":"BMW+CLUB3+"}]'
    '''
    channel_list=eval(channel_str)
    return channel_list

def get_song_list(channel=1, remove_ad=True):
    song_list=[]
    url='http://douban.fm/j/mine/playlist?type=n&channel=%d'% channel
    conn=urllib2.urlopen(url)
    res = conn.read()
    conn.close()
    #print res
    json_dict = eval(res)
    result = int(json_dict['r'])
    if result == 0 :
        play_list=json_dict['song']
        for song in play_list:
            song['url'] = song['url'].replace(r'\/', '/')
            song['picture'] = song['picture'].replace(r'\/', '/')
            if remove_ad and song['url'].find(r'http://img') != -1:
                '''{'album': '\\/radio\\/rdaclick?ad_id=141', 'picture': 'http://img3.douban.com/rda/pic10137.jpg', 'like': '0', 'artist': '', 'url': 'http://img3.douban.com/rda/aud10091.mp3', 'title': 'FM O.U.R', 'subtype': 'T', 'sid': 'da141_10', 'aid': '888141', 'albumtitle': ''}'''
                continue
            else:
                song_list.append(song)
    return song_list

def play(mp3_url, save=False):
    if not save:
        mp3clip = mp3play.load(filename)
        mp3clip.play()
        return mp3clip
def main():
    while True:
        song_list = get_song_list()
        for song in song_list:
            clip = play(song['url'])
            time.slee(clip.seconds())


if __name__ == "__main__":
    channel_list = get_channel_list()
    for c in channel_list:
        print c['channel_id'],
        print c['name']
    song_list = get_song_list()
    if song_list:
        for song in song_list:
            print song['url']
